<?php if (!defined('BASEPATH')) {exit('No direct script access allowed');}

class msupply extends CI_Model {

	function __construct() {
		parent::__construct();
        $ci = & get_instance();
        $ci->load->model('mdealer');		
	}

	function getdata($param="", $param1="", $param2="", $param3=""){
		$select = "";
		$from	= "";
		$where	= "WHERE 1=1 ";
		$join	= "";
		
		//if(isset($this->input->post('field'))){
		//}
		
		switch($param){
			case "pesanan_konsumen":
				$field	= $this->input->post('field');
				$val	= $this->input->post('val');
			
				if($field){
					if($field == 'C.nama_barang'){
						$value=" like '%".$val."%' ";
					}
					$where .=" AND ".$field.$value;
				}
				
				$select .= "A.*, B.nama_dealer, C.nama_barang as nama_generator";
				$from 	.= "tbl_supply_pesanan_generator";
				$join	.= "LEFT JOIN tbl_master_dealer B ON A.id_dealer = B.id ";
				$join	.= "LEFT JOIN tbl_master_barang C ON A.id_generator = C.id ";
			break;
			case "mutasi_generator":
			case "mutasi_promosi":
				$select .= "A.*, B.nama_dealer, C.nama_gudang, D.nama_dealer as nama_dealer_transfer, E.nama_karyawan as nama_pengecek, F.nama_karyawan as nama_pengirim";
				$from 	.= "tbl_supply_mutasi_barang";
				$join	.= "LEFT JOIN tbl_master_dealer B ON A.id_dealer = B.id ";
				$join	.= "LEFT JOIN tbl_master_gudang C ON A.id_gudang = C.id ";
				$join	.= "LEFT JOIN tbl_master_dealer D ON A.id_dealer_transfer = D.id ";
				$join	.= "LEFT JOIN tbl_master_karyawan E ON A.id_pengecek = E.id ";
				$join	.= "LEFT JOIN tbl_master_karyawan F ON A.id_pengirim = F.id ";
				
				if($param == "mutasi_generator") $where .= " AND A.jns_mutasi_barang = '1'"; 
				if($param == "mutasi_promosi") $where .= " AND A.jns_mutasi_barang = '2'"; 
			break;
			case "penerimaan_generator":
			case "penerimaan_promosi":
				$select .= "A.*, B.no_mutasi_barang as no_mutasi, C.nama_karyawan as nama_pengirim, D.nama_karyawan as nama_penerima";
				$from 	.= "tbl_supply_penerimaan_barang";
				$join	.= "LEFT JOIN tbl_supply_mutasi_barang B ON A.id_mutasi = B.id ";
				$join	.= "LEFT JOIN tbl_master_karyawan C ON A.id_pengecek = C.id ";
				$join	.= "LEFT JOIN tbl_master_karyawan D ON A.id_penerima = D.id ";

				if($param == "penerimaan_generator") $where .= " AND A.jns_penerimaan_barang = '1'"; 
				if($param == "penerimaan_promosi") $where .= " AND A.jns_penerimaan_barang = '2'"; 				
			break;
						
			case 'detail_mutasi':
			case 'detail_penerimaan':
				if($param == 'detail_mutasi'){
					$table = 'tbl_supply_detailmutasi_barang';
					$where = "WHERE A.no_mutasi_barang = '$param1'";
				}elseif($param == 'detail_penerimaan'){
					$table = 'tbl_supply_detailpenerimaan_barang';
					$where = "WHERE A.no_penerimaan_barang = '$param1'";
				}
				$sql = "
					SELECT A.*, B.nama_barang
					FROM $table A
					LEFT JOIN tbl_master_barang B ON A.id_barang = B.id
					$where
				";
				$query = $this->db->query($sql)->result_array();
				if($query){
					return $query;
				}else{
					return 0;
				}
			break;
			
		}
		
		$sql = "
				SELECT $select
				FROM $from A
				$join
				$where
		";
		
		return $this->mdealer->get_json($sql); 
	}
	
	function savedatasupply($post, $type="", $editstatus=""){
		$this->db->trans_begin();
		switch($type){
			case 'pesanan_konsumen':
				$table = "tbl_supply_pesanan_generator";
			break;
			case 'mutasi_generator':
			case 'mutasi_promosi':
				if($type == 'mutasi_generator'){
					$post['jns_mutasi_barang'] = '1';
				}elseif($type == 'mutasi_promosi'){
					$post['jns_mutasi_barang'] = '2';
				}
				$table = "tbl_supply_mutasi_barang";
				$count = count($post['id_barang'])-1;
				for($i=0; $i <= $count; $i++){
					if($post['id_barang'][$i] != ''){
						$arraypost = array(
							"jns_detail" => $post['jns_mutasi_barang'],
							"no_mutasi_barang" => $post['no_mutasi_barang'],
							"id_barang" => $post['id_barang'][$i],
							"quantity" => $post['quantity'][$i],
							"satuan" => $post['satuan'][$i],
							"total_nominal" => $post['total_nominal'][$i],
						);
						if($post['parameter'][$i] == 'editnya'){
							$this->db->update('tbl_supply_detailmutasi_barang', $arraypost, array('id'=>$post['id_detail'][$i]));
						}else{
							$this->db->insert('tbl_supply_detailmutasi_barang', $arraypost);
						}
					}
				}
				unset($post['id_barang']);
				unset($post['quantity']);
				unset($post['satuan']);
				unset($post['total_nominal']);
				unset($post['parameter']);
				unset($post['id_detail']);
			break;
			case 'penerimaan_generator':
			case 'penerimaan_promosi':
				if($type == 'penerimaan_generator'){
					$post['jns_penerimaan_barang'] = '1';
				}elseif($type == 'penerimaan_promosi'){
					$post['jns_penerimaan_barang'] = '2';
				}
				$table = "tbl_supply_penerimaan_barang";
				$count = count($post['id_barang'])-1;
				for($i=0; $i <= $count; $i++){
					if($post['id_barang'][$i] != ''){
						$arraypost = array(
							"jns_detail" => $post['jns_penerimaan_barang'],
							"no_penerimaan_barang" => $post['no_penerimaan_barang'],
							"id_barang" => $post['id_barang'][$i],
							"quantity" => $post['quantity'][$i],
							"satuan" => $post['satuan'][$i],
							"total_nominal" => $post['total_nominal'][$i],
							"qty_terima" => $post['qty_terima'][$i],
							"selisih" => $post['selisih'][$i],
							"keterangan" => $post['keterangan'][$i],
						);
						if($post['parameter'][$i] == 'editnya'){
							$this->db->update('tbl_supply_detailpenerimaan_barang', $arraypost, array('id'=>$post['id_detail'][$i]));
						}else{	
							$this->db->insert('tbl_supply_detailpenerimaan_barang', $arraypost);
						}
					}
				}
				unset($post['id_barang']);
				unset($post['quantity']);
				unset($post['satuan']);
				unset($post['total_nominal']);
				unset($post['qty_terima']);
				unset($post['selisih']);
				unset($post['keterangan']);
				unset($post['parameter']);
				unset($post['id_detail']);				
			break;
		}
		
		if($editstatus == 'add'){
			$this->db->insert($table, $post);
		}elseif($editstatus == 'edit'){
			$id = $post['id'];
			unset($post['id']);
			$this->db->update($table, $post, array('id'=>$id));
		}
		
		if($this->db->trans_status() == false){
			$this->db->trans_rollback();
			return "Data not saved";
		}else{
			return $this->db->trans_commit();
		}
		
	}
	
	function getrecord($type, $param1="", $param2="", $param3=""){
		$table = "";
		$select = "";
		$where = "WHERE 1=1 AND A.id = '".$param1."'";
		$join = "";
		switch($type){
			case 'pesanan_konsumen':
				$select .= "A.*, B.nama_dealer, C.nama_barang";
				$table .= "tbl_supply_pesanan_generator";
				$join .= " LEFT JOIN tbl_master_dealer B ON A.id_dealer = B.id ";
				$join .= " LEFT JOIN tbl_master_barang C ON A.id_generator = C.id ";
			break;
			case 'mutasi_generator':
			case 'mutasi_promosi':
				$select .= "A.*, B.nama_dealer, C.nama_gudang, D.nama_dealer as dealer_tujuan,
								E.nama_karyawan as nama_pengecek, F.nama_karyawan as nama_pengirim
							";
				$table .= "tbl_supply_mutasi_barang";
				$join .= " LEFT JOIN tbl_master_dealer B ON A.id_dealer = B.id ";
				$join .= " LEFT JOIN tbl_master_gudang C ON A.id_gudang = B.id ";
				$join .= " LEFT JOIN tbl_master_dealer D ON A.id_dealer_transfer = D.id ";
				$join .= " LEFT JOIN tbl_master_karyawan E ON A.id_pengecek = E.id ";
				$join .= " LEFT JOIN tbl_master_karyawan F ON A.id_pengirim = F.id ";
			break;
			case 'penerimaan_generator':
			case 'penerimaan_promosi':
				$select .= "A.*, B.nama_dealer, C.no_mutasi_barang, C.tgl_mutasi, F.nama_karyawan as nama_pengirim,
								D.nama_karyawan as nama_pengecek, E.nama_karyawan as nama_penerima
							";
				$table .= "tbl_supply_penerimaan_barang";
				$join .= " LEFT JOIN tbl_master_dealer B ON A.id_dealer = B.id ";
				$join .= " LEFT JOIN tbl_supply_mutasi_barang C ON A.id_mutasi = B.id ";
				$join .= " LEFT JOIN tbl_master_karyawan D ON A.id_pengecek = D.id ";
				$join .= " LEFT JOIN tbl_master_karyawan E ON A.id_penerima = E.id ";
				$join .= " LEFT JOIN tbl_master_karyawan F ON C.id_pengirim = F.id ";
			break;
		}
		
		$sql = "
			SELECT $select
			FROM $table A
			$join
			$where
		";
		
		return $this->db->query($sql)->row();
	}
	
	function getcombo($type="", $param=""){
		
	}
	
	
}